from django.db import models

# Create your models here.
# 品牌表
# id,    first_name,  name,       logo
# pk,    varchar(10)  varchar(20) varchar(200)
#        默认 None

# 品牌表
class Brand(models.Model):
    first_name = models.CharField('品牌首字母', max_length=10, default=None)
    name = models.CharField('品牌名称', max_length=20, )
    logo = models.CharField('品牌logo', max_length=200, )
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'brand'
        verbose_name_plural = '品牌表'


# 种类表
class Cate(models.Model):
    name = models.CharField('种类名称', max_length=30)
    cid = models.ForeignKey('self', on_delete=models.CASCADE, related_name='subs', null=True, blank=True)

    def __str__(self):
        return self.name
    class Meta:
        db_table = 'cate'
        verbose_name_plural = '商品种类表'


# 频道组
class Group(models.Model):
    name = models.CharField('频道组名称', max_length=30)
    cate = models.OneToOneField(Cate, on_delete=models.CASCADE)
    group_url = models.CharField('频道组的链接', max_length=100, default='http://jd.com')
    sequence = models.IntegerField('频道组的展示顺序', default=0)

    def __str__(self):
        return self.name
    class Meta:
        db_table = 'cate_group'
        verbose_name_plural = '频道组'


# SPU
class SPU(models.Model):
    name = models.CharField('spu', max_length=30)
    brand = models.ForeignKey(Brand, on_delete=models.CASCADE, verbose_name='品牌')
    category1 = models.ForeignKey(Cate, on_delete=models.PROTECT, verbose_name='一级类别', related_name='cate1_spu')
    category2 = models.ForeignKey(Cate, on_delete=models.PROTECT, verbose_name='二级类别', related_name='cate2_spu')
    category3 = models.ForeignKey(Cate, on_delete=models.PROTECT, verbose_name='三级类别', related_name='cate3_spu')
    comment_num = models.IntegerField('评论量', default=0)
    sales = models.IntegerField('销量', default=0)
    desc_detail = models.TextField('详细描述', default='')
    desc_pack = models.TextField('包装信息', default='')
    desc_service = models.TextField('售后服务', default='')

    def __str__(self):
        return self.name
    class Meta:
        db_table = 'spu'
        verbose_name_plural = 'SPU表'


# 商品表SKU
class Good(models.Model):
    name = models.CharField('商品名', max_length=100)
    describe = models.CharField('商品描述', max_length=500)
    price = models.DecimalField('价格', max_digits=8,decimal_places=2)
    stock = models.IntegerField('库存', default=0)
    sales = models.IntegerField('销量', default=0)
    detail = models.TextField('详情', default='')
    image_default = models.CharField('默认图片', max_length=300)
    is_selling = models.BooleanField('是否在售', default=True)
    cate = models.ForeignKey(Cate, on_delete=models.CASCADE, verbose_name='商品类别')
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE, verbose_name='商品的SPU')
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'sku'
        verbose_name_plural = '商品SKU表'

# SPU规格表
class SPUSpecification(models.Model):
    name = models.CharField('规格名称', max_length=30)
    spu = models.ForeignKey(SPU, on_delete=models.CASCADE, verbose_name='商品SPU')
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'spu_specification'
        verbose_name_plural = 'SPU规格'

#规格选项值
class SpecOption(models.Model):
    value = models.CharField('选项值', max_length=30)
    spec = models.ForeignKey(SPUSpecification, on_delete=models.CASCADE,
                             verbose_name='规格')
    def __str__(self):
        return '%s:%s'%(self.spec.name, self.value)

    class Meta:
        db_table = 'spec_option'

# sku规格
class SKUSpec(models.Model):
    sku = models.ForeignKey(Good, on_delete=models.CASCADE,
                            verbose_name='SKU商品')
    spec = models.ForeignKey(SPUSpecification, on_delete=models.CASCADE,
                             verbose_name='SPU规格名称')
    option = models.ForeignKey(SpecOption, on_delete=models.CASCADE,
                               verbose_name='规格选项值')


    def __str__(self):
        return '%s:%s-%s' % (self.sku.name, self.spec.name, self.option.value)

    class Meta:
        db_table = 'sku_spec_option'

# sku图片
class SKUImage(models.Model):
    sku = models.ForeignKey(Good, on_delete=models.CASCADE,
                            verbose_name='sku商品')
    image = models.CharField('商品图片', max_length=300,)

    def __str__(self):
        return '%s:%s' % (self.sku.name, self.image)

    class Meta:
        db_table = 'sku_image'







